package com.rc.saas.tenant.service.ticket;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.common.exception.CustomException;
import com.rc.saas.tenant.common.mq.MqProducer;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.elec.ElecStationMapper;
import com.rc.saas.tenant.mapper.ticket.*;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.model.member.MemberInfoExample;
import com.rc.saas.tenant.model.member.RcMemberFlag;
import com.rc.saas.tenant.model.member.RcMemberFlagExample;
import com.rc.saas.tenant.model.ticket.*;
import com.rc.saas.tenant.search.ticket.TicketSearch;
import com.rc.saas.tenant.service.member.MemberInfoService;
import com.rc.saas.tenant.service.member.RcMemberFlagService;
import com.rc.saas.tenant.vo.ticket.GiveTicketDto;
import com.rc.saas.tenant.vo.ticket.TenantTicketTypeDto;
import com.rc.saas.tenant.vo.ticket.TicketVo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author frog
 * @date 2021/2/25
 */
@Service
public class TenantTicketTypeServiceImpl implements TenantTicketTypeService {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TenantTicketTypeMapper tenantTicketTypeMapper;

    @Autowired
    private MemberInfoService memberInfoService;

    @Autowired
    private MemberTicketMapper memberTicketMapper;

    @Autowired
    private ElecStationMapper elecStationMapper;

    @Autowired
    private TenantTicketStationMapper tenantTicketStationMapper;

    @Autowired
    private TenantTicketScheduleMapper tenantTicketScheduleMapper;

    @Autowired
    private TenantTicketSendRecordMapper tenantTicketSendRecordMapper;

    @Autowired
    private MqProducer mqProducer;

    @Autowired
    private RcMemberFlagService memberFlagService;

    @Override
    public TenantTicketTypeMapper getMapper() {
        return tenantTicketTypeMapper;
    }

    @Override
    public PageInfo listPageTicket(TicketSearch search) {
        PageHelper.startPage(search.getOffset(), search.getLimit());
        List<TicketVo> list = tenantTicketTypeMapper.listTicket(search);

        for (TicketVo ticket : list) {
            StringBuffer discountDesc = new StringBuffer("满");
            if ("0".equals(ticket.getDiscountType())) {
                discountDesc.append(ticket.getUseRequireAmount()).append("减").append(ticket.getDiscountAmount());

            } else {
                discountDesc.append(ticket.getUseRequireAmount()).append("打").append(ticket.getDiscount().multiply(BigDecimal.TEN)).append("折");
            }
            ticket.setDiscountDesc(discountDesc.toString());


            if ("1".equals(ticket.getStatus())) {
                ticket.setStatusDesc("已作废");
            } else {
                if ("1".equals(ticket.getEffectType())) {
                    if (new Date().before(ticket.getEffectStartDate())) {
                        ticket.setStatusDesc("未开始");
                    } else if (new Date().after(ticket.getEffectEndDate())) {
                        ticket.setStatusDesc("已发放");
                    } else {
                        ticket.setStatusDesc("发放中");
                    }
                } else {
                    ticket.setStatusDesc("发放中");
                }
            }

            if ("0".equals(ticket.getEffectType())) {
                ticket.setEffectDateDesc("获得起" + ticket.getEffectDay() + "天内有效");

            } else {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String effectiveStartDate = dateFormat.format(ticket.getEffectStartDate());
                String effectiveEndDate = dateFormat.format(ticket.getEffectEndDate());
                ticket.setEffectDateDesc(effectiveStartDate + "-" + effectiveEndDate);
            }
        }
        
        return new PageInfo<>(list);
    }

    @Override
    public TResponse saveGiveAway(GiveTicketDto giveTicketDTO) throws CustomException {
        Date date = new Date();
        //发放记录
        TenantTicketSendRecord tenantTicketSendRecord = new TenantTicketSendRecord();
        tenantTicketSendRecord.setTenantCode(giveTicketDTO.getTenantCode());
        tenantTicketSendRecord.setTicketTypeId(giveTicketDTO.getTicketTypeId());
        tenantTicketSendRecord.setSendTime(date);
        TenantTicketType tenantTicketType = tenantTicketTypeMapper.selectByPrimaryKey(giveTicketDTO.getTicketTypeId());

        logger.info("保存普通优惠券,运营商:{},(手动输入)赠送优惠券的用户为:{},(上传用户):",giveTicketDTO.getTenantCode(),giveTicketDTO.getMembers(),giveTicketDTO.getMemberIds());

        //判断是单次赠送还是多次赠送
        if ("0".equals(giveTicketDTO.getSendType())) {
            if ("on".equals(giveTicketDTO.getSendRegularly())) {
                if ("0".equals(giveTicketDTO.getUserType())) {
                    TResponse tResponse = saveSendRegularly(giveTicketDTO, tenantTicketSendRecord, tenantTicketType);
                    return tResponse;
                } else {
                    saveSendRegularly(giveTicketDTO, tenantTicketSendRecord, tenantTicketType);
                }
            } else {
                List<MemberTicket> memberTicketList = new LinkedList();

                tenantTicketSendRecord.setSendType("0");

                if ("0".equals(giveTicketDTO.getUserType())) {
                    String[] moblies = giveTicketDTO.getMembers().split(",");

                    if (tenantTicketType.getRemainTicket() < giveTicketDTO.getSendCount() * moblies.length) {
                        throw new CustomException("优惠券数量不足");
                    }
                    tenantTicketType.setRemainTicket(tenantTicketType.getRemainTicket() - giveTicketDTO.getSendCount() * moblies.length);
                    tenantTicketTypeMapper.updateByPrimaryKeySelective(tenantTicketType);

                    for (String moblie : moblies) {
                        MemberInfo memberInfo = memberInfoService.selectMemberByMobile(giveTicketDTO.getTenantCode(), moblie);
                        if (memberInfo != null) {
                            MemberTicket memberTicket = saveMemberTicket(giveTicketDTO, date, tenantTicketSendRecord, tenantTicketType);
                            memberTicket.setMemberId(memberInfo.getMemberId());
                            memberTicketList.add(memberTicket);
                        }
                    }

                    tenantTicketSendRecord.setSendCount(giveTicketDTO.getSendCount() * memberTicketList.size());
                    tenantTicketSendRecordMapper.insertReturnId(tenantTicketSendRecord);
                    //文件上传方式
                } else {
                    if (tenantTicketType.getRemainTicket() < giveTicketDTO.getSendCount() * giveTicketDTO.getMemberIds().size()) {
                        throw new CustomException("优惠券数量不足");
                    }
                    tenantTicketType.setRemainTicket(tenantTicketType.getRemainTicket() - giveTicketDTO.getSendCount() * giveTicketDTO.getMemberIds().size());
                    tenantTicketTypeMapper.updateByPrimaryKeySelective(tenantTicketType);

                    for (Long memberId : giveTicketDTO.getMemberIds()) {
                        MemberTicket memberTicket = saveMemberTicket(giveTicketDTO, date, tenantTicketSendRecord, tenantTicketType);
                        memberTicket.setMemberId(memberId);
                        memberTicketList.add(memberTicket);
                    }

                    tenantTicketSendRecord.setSendCount(giveTicketDTO.getSendCount() * memberTicketList.size());
                    tenantTicketSendRecordMapper.insertReturnId(tenantTicketSendRecord);
                }
                if (memberTicketList.size() == 0) {
                    throw new CustomException("输入的所有用户有误,或没有输入");
                }
                for (MemberTicket memberTicket:memberTicketList){
                    memberTicket.setTicketSendRecordId(tenantTicketSendRecord.getTicketSendRecordId());
                }
                Integer sendCount = giveTicketDTO.getSendCount();
                for (int i = 0; i < sendCount - 1; i++) {
                    memberTicketMapper.insertBatch(memberTicketList);
                }
                memberTicketMapper.insertBatch(memberTicketList);
            }
        } else if ("1".equals(giveTicketDTO.getSendType())) {
            saveSendRegularly(giveTicketDTO, tenantTicketSendRecord, tenantTicketType);
        }
        return TResponse.SUCCESS();
    }

    private MemberTicket saveMemberTicket(GiveTicketDto giveTicketDTO, Date date, TenantTicketSendRecord tenantTicketSendRecord, TenantTicketType tenantTicketType) {
        MemberTicket memberTicket = new MemberTicket();
        memberTicket.setTenantCode(giveTicketDTO.getTenantCode());
        memberTicket.setTicketTypeId(giveTicketDTO.getTicketTypeId());
        //优惠券来源 0来自赠送,默认0
        memberTicket.setTicketSource("0");
        //优惠券状态 0未使用 1已使用 默认0
        memberTicket.setStatus("0");
//        memberTicket.setTicketSendRecordId(tenantTicketSendRecord.getTicketSendRecordId());

        if ("0".equals(tenantTicketType.getEffectType())) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_YEAR, tenantTicketType.getEffectDay());
            Date time = calendar.getTime();
            memberTicket.setGainTime(date);
            memberTicket.setEndTime(time);
        } else if ("1".equals(tenantTicketType.getEffectType())) {
            memberTicket.setGainTime(date);
            memberTicket.setEndTime(tenantTicketType.getEffectEndDate());
        }
        return memberTicket;
    }

    private TResponse saveSendRegularly(GiveTicketDto giveTicketDTO, TenantTicketSendRecord tenantTicketSendRecord, TenantTicketType tenantTicketType) throws CustomException {
        List<TenantTicketSchedule> tenantTicketSchedules = new LinkedList<>();
        String[] moblies = giveTicketDTO.getMembers().split(",");

        tenantTicketSendRecord.setSendType("1");

        logger.info("保存定时优惠券,运营商:{},(手动输入)赠送优惠券的用户为:{},(上传用户):",giveTicketDTO.getTenantCode(),giveTicketDTO.getMembers(),giveTicketDTO.getMemberIds());

        if ("0".equals(giveTicketDTO.getUserType())) {
            //单次
            if ("0".equals(giveTicketDTO.getSendType())) {
                if (tenantTicketType.getRemainTicket() < giveTicketDTO.getSendCount()  * moblies.length) {
                    throw new CustomException("优惠券数量不足");
                }
                tenantTicketType.setRemainTicket(tenantTicketType.getRemainTicket() - giveTicketDTO.getSendCount()  * moblies.length);
                tenantTicketTypeMapper.updateByPrimaryKeySelective(tenantTicketType);
                tenantTicketSendRecord.setSendCount(giveTicketDTO.getSendCount() *  moblies.length);
            } else {
                if (tenantTicketType.getRemainTicket() < giveTicketDTO.getTicketScheduleCount() * giveTicketDTO.getSendNum() * moblies.length) {
                    throw new CustomException("优惠券数量不足");
                }
                tenantTicketType.setRemainTicket(tenantTicketType.getRemainTicket() - giveTicketDTO.getTicketScheduleCount() * giveTicketDTO.getSendNum() * moblies.length);
                tenantTicketTypeMapper.updateByPrimaryKeySelective(tenantTicketType);
                tenantTicketSendRecord.setSendCount(giveTicketDTO.getTicketScheduleCount() * giveTicketDTO.getSendNum() * moblies.length);
            }



            for (String moblie : moblies) {
                MemberInfo memberInfo = memberInfoService.selectMemberByMobile(giveTicketDTO.getTenantCode(), moblie);
                if (memberInfo != null) {
                    TenantTicketSchedule tenantTicketSchedule = saveTicketSchedule(giveTicketDTO, tenantTicketSendRecord);

                    tenantTicketSchedule.setMemberId(memberInfo.getMemberId());
                    tenantTicketSchedule.setTicketSendRecordId(tenantTicketSendRecord.getTicketSendRecordId());
                    tenantTicketSchedules.add(tenantTicketSchedule);
                }
            }

            tenantTicketSendRecordMapper.insertReturnId(tenantTicketSendRecord);
            if (tenantTicketSchedules.size() == 0) {
                throw new CustomException("输入的所有用户有误,或没有输入");
            }
        } else {

            if ("0".equals(giveTicketDTO.getSendType())) {
                if (tenantTicketType.getRemainTicket() < giveTicketDTO.getSendCount() * giveTicketDTO.getMemberIds().size()) {
                    throw new CustomException("优惠券数量不足");
                }
                tenantTicketType.setRemainTicket(tenantTicketType.getRemainTicket() - giveTicketDTO.getSendCount() * giveTicketDTO.getMemberIds().size());
                tenantTicketTypeMapper.updateByPrimaryKeySelective(tenantTicketType);
                tenantTicketSendRecord.setSendCount(giveTicketDTO.getSendCount() * giveTicketDTO.getMemberIds().size());
            } else {
                if (tenantTicketType.getRemainTicket() < giveTicketDTO.getTicketScheduleCount() * giveTicketDTO.getSendNum() * giveTicketDTO.getMemberIds().size()) {
                    throw new CustomException("优惠券数量不足");
                }
                tenantTicketType.setRemainTicket(tenantTicketType.getRemainTicket() - giveTicketDTO.getTicketScheduleCount() * giveTicketDTO.getSendNum() * giveTicketDTO.getMemberIds().size());
                tenantTicketTypeMapper.updateByPrimaryKeySelective(tenantTicketType);
                tenantTicketSendRecord.setSendCount(giveTicketDTO.getTicketScheduleCount() * giveTicketDTO.getSendNum() * giveTicketDTO.getMemberIds().size());
            }

            tenantTicketSendRecordMapper.insertReturnId(tenantTicketSendRecord);

            for (Long memberId : giveTicketDTO.getMemberIds()) {
                TenantTicketSchedule tenantTicketSchedule = saveTicketSchedule(giveTicketDTO, tenantTicketSendRecord);
                tenantTicketSchedule.setTicketSendRecordId(tenantTicketSendRecord.getTicketSendRecordId());
                tenantTicketSchedule.setMemberId(memberId);
                //   tenantTicketSchedule.setMemberMobile(memberInfo.getMobile());
                tenantTicketSchedules.add(tenantTicketSchedule);
            }
        }
        tenantTicketScheduleMapper.insertBatch(tenantTicketSchedules);
        return TResponse.SUCCESS();
    }

    private TenantTicketSchedule saveTicketSchedule(GiveTicketDto giveTicketDTO, TenantTicketSendRecord tenantTicketSendRecord) {
        TenantTicketSchedule tenantTicketSchedule = new TenantTicketSchedule();
        tenantTicketSchedule.setTenantCode(giveTicketDTO.getTenantCode());
        tenantTicketSchedule.setTicketTypeId(giveTicketDTO.getTicketTypeId());
        tenantTicketSchedule.setStatus("0");

        tenantTicketSchedule.setTicketSendRecordId(tenantTicketSendRecord.getTicketSendRecordId());
        if ("0".equals(giveTicketDTO.getSendType())) {
            tenantTicketSchedule.setPeriodCount(giveTicketDTO.getSendCount());
            tenantTicketSchedule.setFirstSendTime(giveTicketDTO.getOnceSendTime());
            tenantTicketSchedule.setRemainCount(1);
            tenantTicketSchedule.setTicketScheduleCount(1);
            tenantTicketSchedule.setSchedulePeriod("0");
            tenantTicketSchedule.setSendType("0");
        } else {
            tenantTicketSchedule.setFirstSendTime(giveTicketDTO.getFirstSendTime());
            tenantTicketSchedule.setSchedulePeriod(giveTicketDTO.getSchedulePeriod());
            tenantTicketSchedule.setTicketScheduleCount(giveTicketDTO.getSendNum());
            tenantTicketSchedule.setPeriodCount(giveTicketDTO.getTicketScheduleCount());
            tenantTicketSchedule.setRemainCount(giveTicketDTO.getSendNum());
            tenantTicketSchedule.setSendType("1");
        }
        return tenantTicketSchedule;
    }

    @Override
    public void saveOrUpdateTenantTicketType(TenantTicketTypeDto tenantTicketTypeDTO) {
        if (tenantTicketTypeDTO.getTicketTypeId() == null) {
            tenantTicketTypeDTO.setRemainTicket(tenantTicketTypeDTO.getTotalTicket());
            tenantTicketTypeDTO.setCreatedTime(new Date());
            tenantTicketTypeMapper.insertSelectiveReturnId(tenantTicketTypeDTO);
        } else {
            tenantTicketTypeMapper.updateByPrimaryKeySelective(tenantTicketTypeDTO);

            TenantTicketType ticketType = tenantTicketTypeMapper.selectByPrimaryKey(tenantTicketTypeDTO.getTicketTypeId());
            tenantTicketTypeDTO.setRemainTicket(ticketType.getRemainTicket());
        }



        TenantTicketStationExample example = new TenantTicketStationExample();
        example.createCriteria().andTicketTypeIdEqualTo(tenantTicketTypeDTO.getTicketTypeId());
        //原有优惠券已设置的站点
        List<TenantTicketStation> tenantTicketStations = tenantTicketStationMapper.selectByExample(example);

        //取消优惠券设置站点
        List<Long> elecStationIds1 = new ArrayList<>();
        //新增优惠券设置站点
        List<Long> elecStationIds2 = new ArrayList<>();

        List<Long> elecStationIds = tenantTicketStations.stream().map(tenantTicketStation -> tenantTicketStation.getElecStationId()).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(elecStationIds)) {
            elecStationIds1 = (List<Long>) CollectionUtils.subtract(elecStationIds, tenantTicketTypeDTO.getStationIds());
            if (!CollectionUtils.isEmpty(elecStationIds1)) {
                List<TenantTicketStation> tenantTicketStationList = tenantTicketStationMapper.listTicketStationByStationIdAndTicketType(elecStationIds1, tenantTicketTypeDTO.getTicketTypeId());
                if (!CollectionUtils.isEmpty(tenantTicketStationList)) {
                    for (TenantTicketStation tenantTicketStation : tenantTicketStationList) {
                        tenantTicketStationMapper.deleteByPrimaryKey(tenantTicketStation.getTicketStationId());
                    }
                }
            }
        }
        if (!CollectionUtils.isEmpty(tenantTicketTypeDTO.getStationIds())) {
            elecStationIds2 = (List<Long>) CollectionUtils.subtract(tenantTicketTypeDTO.getStationIds(), elecStationIds);
        }

        TenantTicketStation tenantTicketStation = new TenantTicketStation();
        tenantTicketStation.setTenantCode(tenantTicketTypeDTO.getTenantCode());
        tenantTicketStation.setTicketTypeId(tenantTicketTypeDTO.getTicketTypeId());

        for (Long stationId : elecStationIds2) {
            ElecStation elecStation = elecStationMapper.selectByPrimaryKey(stationId);
            tenantTicketStation.setElecStationId(elecStation.getElecStationId());
            if (elecStation.getHlhtOperatorId() != null) {
                tenantTicketStation.setHlhtOperatorId(elecStation.getHlhtOperatorId());
            } else if (tenantTicketTypeDTO.getTenantCode() != tenantTicketStation.getTenantCode()) {
                tenantTicketStation.setHlhtTenantCode(tenantTicketStation.getTenantCode());
            }
            tenantTicketStation.setCreatedTime(new Date());
            tenantTicketStationMapper.insertSelective(tenantTicketStation);
        }


        if ("0".equals(tenantTicketTypeDTO.getTicketSource())) {
            List<Long> memberIdList = listMemberForSendTicket(tenantTicketTypeDTO.getApplianceUser(), tenantTicketTypeDTO.getApplicnceLevel());
            if (memberIdList == null || memberIdList.size() < 1) {
                return;
            } else {
                Integer remain = tenantTicketTypeDTO.getRemainTicket();
                Long ticketId = tenantTicketTypeDTO.getTicketTypeId();

                Date endTime = null;
                if ("1".equals(tenantTicketTypeDTO.getEffectType())) {
                    endTime = tenantTicketTypeDTO.getEffectEndDate();
                } else if ("0".equals(tenantTicketTypeDTO.getEffectType())) {
                    LocalDateTime localDateTime = LocalDateTime.now();
                    LocalDateTime endDateTime = localDateTime.plusDays(tenantTicketTypeDTO.getEffectDay());
                    endTime = Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant());
                }

                MemberTicketExample memberTicketExample = new MemberTicketExample();
                memberTicketExample.createCriteria().andMemberIdIn(memberIdList).andTicketTypeIdEqualTo(ticketId);
                Set<Long> memberIdSet = memberTicketMapper.selectByExample(memberTicketExample).stream().map(MemberTicket::getMemberId).collect(Collectors.toSet());


                for (Long memberId : memberIdList) {
                    if (remain > 0) {
                        if (memberIdSet.contains(memberId)) {
                            continue;
                        }
                        TenantTicketSendRecord ticketSendRecord = new TenantTicketSendRecord();
                        ticketSendRecord.setTicketTypeId(ticketId);
                        ticketSendRecord.setSendCount(1);
                        ticketSendRecord.setSendTime(new Date());
                        ticketSendRecord.setSendType("0");
                        tenantTicketSendRecordMapper.insertReturnId(ticketSendRecord);

                        MemberTicket memberTicket = new MemberTicket();
                        memberTicket.setMemberId(memberId);
                        memberTicket.setTicketTypeId(ticketId);
                        memberTicket.setTicketSendRecordId(ticketSendRecord.getTicketSendRecordId());
                        memberTicket.setTicketSource("0");

                        memberTicket.setEndTime(endTime);
                        memberTicket.setGainTime(new Date());
                        memberTicket.setStatus("0");
                        memberTicket.setCreatedTime(new Date());
                        memberTicketMapper.insertSelective(memberTicket);
                        remain--;

                    } else {
                        break;
                    }
                }

                tenantTicketTypeDTO.setRemainTicket(remain);
                tenantTicketTypeMapper.updateByPrimaryKeySelective(tenantTicketTypeDTO);
            }

//            Date date = new Date();
//            int second = (int)((tenantTicketTypeDTO.getSendDate().getTime() - date.getTime())/1000);
//
//            if (second < 1) {
//                second = 1;
//            }
//
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("ticketId", tenantTicketTypeDTO.getTicketTypeId());
//            map.put("memberIdList", memberIdList);
//            mqProducer.send("send_ticket", JSON.toJSONString(map), second);
        }


    }

    @Override
    public List<TenantTicketType> listTenantTicketType(String tenantCode) {
        TenantTicketTypeExample example = new TenantTicketTypeExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode);
        example.setOrderByClause("ticket_type_id desc");
        List<TenantTicketType> tenantTicketTypes = tenantTicketTypeMapper.selectByExample(example);
        return tenantTicketTypes;
    }

    @Override
    public List<TicketVo> getTenantTickets(TicketSearch search) {
        List<TicketVo> list = tenantTicketTypeMapper.listTicket(search);
        for (TicketVo ticket : list) {
            StringBuffer discountDesc = new StringBuffer("满");
            if ("0".equals(ticket.getDiscountType())) {
                discountDesc.append(ticket.getUseRequireAmount()).append("减").append(ticket.getDiscountAmount());
            } else {
                discountDesc.append(ticket.getUseRequireAmount()).append("打").append(ticket.getDiscount().multiply(BigDecimal.TEN)).append("折");
            }
            ticket.setDiscountDesc(discountDesc.toString());
            switch (ticket.getTicketSuitType()){
                case "0":
                    ticket.setDiscountTypeName("充电减免");
                    break;
                case "1":
                    ticket.setDiscountTypeName("服务费减免");
                    break;
                case "2":
                    ticket.setDiscountTypeName("充电满折");
                    break;
                case "3":
                    ticket.setDiscountTypeName("服务费满折");
                    break;
                case "4":
                    ticket.setDiscountTypeName("代金券");
                    break;
            }
            switch (ticket.getApplianceUser()){
                case "0":
                    ticket.setUserTypeName("全部用户");
                    break;
                case "1":
                    ticket.setUserTypeName("新用户");
                    break;
                case "9":
                    ticket.setUserTypeName("等级用户");
                    break;
                case "10":
                    ticket.setUserTypeName("企业用户");
                    break;
            }
        }
        return list;
    }


    private List<Long> listMemberForSendTicket(String applicanceUser, String applicnceLvel) {
        if ("0".equals(applicanceUser)) {
            MemberInfoExample example = new MemberInfoExample();
            List<Long> list = memberInfoService.selectByExample(example).stream().map(MemberInfo::getMemberId).collect(Collectors.toList());
            return list;
        } else if ("9".equals(applicanceUser)) {
            RcMemberFlagExample example = new RcMemberFlagExample();
            example.createCriteria().andIsMemberLevelEqualTo("1").andMemberLevelEqualTo(applicnceLvel);
            List<Long> list = memberFlagService.selectByExample(example).stream().map(RcMemberFlag::getMemberId).collect(Collectors.toList());
            return list;
        } else if ("10".equals(applicanceUser)) {
            RcMemberFlagExample example = new RcMemberFlagExample();
            example.createCriteria().andIsEnterpriseLevelEqualTo("1");
            List<Long> list = memberFlagService.selectByExample(example).stream().map(RcMemberFlag::getMemberId).collect(Collectors.toList());
            return list;
        }
        return null;
    }
}
